//
//  GConfig.m
//  FootPrint
//
//  Created by alex tao on 2/25/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "GConfig.h"
#import "LoginViewController.h"

#define kLoginInfoKey       @"keyLoginInfo"
#define kUserIdKey          @"keyUserId"

@implementation GConfig

+ (BOOL)hasLogin;
{
    if ([GConfig getUserId] > 0) {
        return YES;
    }
    return NO;
}

+ (void)logout
{
    [GConfig setUserId:0];
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_LOGOUT object:nil];
}

+ (NSDictionary *)getUserLoginInfo
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kLoginInfoKey];
}

+ (void)setUserLoginInfo:(NSDictionary *)info
{
    if (info) {
        [[NSUserDefaults standardUserDefaults] setObject:info forKey:kLoginInfoKey];
    } else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kLoginInfoKey];
    }
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (long long)getUserId
{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:kUserIdKey] longLongValue];
}

+ (void)setUserId:(long long)uid
{
    if (uid > 0) {
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithLongLong:uid] forKey:kUserIdKey];
    } else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserIdKey];
    }
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
